package com.whl.sm.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.dto.SelectDto;
import com.whl.sm.dto.TreeDto;
import com.whl.sm.model.system.SysDict;
import com.whl.sm.service.system.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典表 前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-09-18
 */
@RestController
@RequestMapping("/sys-dict")
@Slf4j
public class SysDictController extends BaseController {
    @Autowired
    private SysDictService sysDictService;

    @RequestMapping("/list")
    public Result getDictPages(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("dict_value");
        queryWrapper.orderByDesc("update_time");
        Page<SysDict> dictPage = sysDictService.page(getPage(),queryWrapper);
        dictPage.getRecords().forEach(obj ->
                obj.setParentValue(obj.getParentId().equals("0")
                        ?null:
                        sysDictService.getById(obj.getParentId()).getDictValue())
                );
        return Result.ok(dictPage);
    }


    @RequestMapping("/info/{id}")
    public Result dictInfo(@PathVariable("id") String id){
        SysDict sysDict = sysDictService.getById(id);
        SysDict parentDict  = sysDictService.getById(sysDict.getParentId());
        sysDict.setParentValue(parentDict==null?null:parentDict.getDictValue());
        return  Result.ok(sysDict);
    }

    @RequestMapping("/parentInfo")
    public Result getDictParentChose(@RequestParam String likeName){
        List<SysDict> pLists = sysDictService.list(new QueryWrapper<SysDict>().like("dict_value",likeName));
        return Result.ok(convert(pLists));
    }

    @RequestMapping("/dictKey")
    public Result getByDictKey(@RequestParam String dictKey){
        List<SysDict> dictList = sysDictService.list(new QueryWrapper<SysDict>().eq("dict_key",dictKey));
        return Result.ok(dictList);
    }

    @RequestMapping("/dictSelectByKey")
    public Result getDictSelectByKey(@RequestParam String dictKey){
        List<SysDict> dictList = sysDictService.list(new QueryWrapper<SysDict>().eq("dict_key",dictKey));
        List<SelectDto> result =  dictList.stream().map(obj->{
            return new SelectDto(obj.getId(),obj.getDictValue(),null,false);
        }).collect(Collectors.toList());
        return Result.ok(result);
    }


    @RequestMapping("/update")
    public Result updateDict(@RequestBody SysDict sysDict){
        sysDictService.update(sysDict);
        return Result.ok();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestBody List<String> idList){
        try {
            for (int i = 0; i <idList.size() ; i++) {
                sysDictService.removeById(idList.get(i));
            }
        }
        catch(Exception e){
            return Result.fail();
        }
        return Result.ok();
    }


    //用户菜单权限分配树转换
    private List<TreeDto> convert(List<SysDict> menus) {
        List<TreeDto> permMenuDtos = new ArrayList<>();
        menus.forEach(m -> {
            TreeDto dto = new TreeDto();
            dto.setId(m.getId());
            dto.setLabel(m.getDictValue());
            if (m.getChildren().size() > 0) {
                dto.setChildren(convert(m.getChildren()));
            }
            else {
                m.setChildren(null);
            }
            permMenuDtos.add(dto);
        });
        return permMenuDtos;
    }

}
